const path = require('path');
const fs = require('fs');

const viewsJson = {};

function pathResolve(url) {
    return path.resolve(__dirname, '../', url);
}

function getViewsContent(url) {
    const content = fs.readFileSync(pathResolve(url)).toString();
    content.split(/\r|\n|\r\n/).forEach(i => {
        const [_, key = null] = i.match(/allPage\[['|"](.+)['|"]\]/) || [];

        if (key) {
            viewsJson[key] = (i.match(/\/\/(\s*)?(.*)/) || [])[2] || '';
        }
    });
}

function allView(url) {
    const stat = fs.statSync(pathResolve(url));

    if (stat.isDirectory()) {
        const fileNames = fs.readdirSync(pathResolve(url));
        fileNames
            .filter(i => (!/^\./.test(i)))
            .filter(i => (!/^activity$/.test(i))) // 过滤;
            .forEach(i => {
                const newI = `${url}/${i}`;
                if (i === 'View.js') {
                    getViewsContent(newI);
                } else if (fs.statSync(pathResolve(newI)).isDirectory()) {
                    allView(newI);
                }
            });
    }
}


const [_, __, importPath = 'src/views', exportPath = './export.json'] = process.argv;

allView(importPath);

fs.writeFileSync(path.resolve(__dirname, exportPath), JSON.stringify(viewsJson, null, 4));
